@(repository: gitbucket.core.service.RepositoryService.RepositoryInfo,
  tag: gitbucket.core.util.JGitUtil.TagInfo,
  tags: Seq[String],
  content: String,
  release: Option[(gitbucket.core.model.ReleaseTag, Seq[gitbucket.core.model.ReleaseAsset])])(implicit context: gitbucket.core.controller.Context)
@import gitbucket.core.view.helpers
@gitbucket.core.html.main(s"New Release - ${repository.owner}/${repository.name}", Some(repository)){
  @gitbucket.core.html.menu("releases", repository){
    <form method="POST" validate="true" class="form-group" autocomplete="off">
      <div class="row-fluid">
        <div class="co`l-md-12">
          @if(release.isEmpty){
            <h3>New release for @tag.name</h3>
          } else {
            <h3>Update release for @tag.name</h3>
          }
          <span id="error-name" class="error"></span>
          <input type="text" id="release-name" name="name" class="form-control" value="@(release.map { case (release, _) => release.name }.getOrElse(tag.name))" placeholder="Title" style="margin-bottom: 6px;" autofocus aria-label="Release name"/>
          <div class="pull-right">
            <label for="insert-changelog-tag">Previous tag: </label>
            <select id="insert-changelog-tag">
              @tags.map { tag =>
                <option value="@tag">@tag</option>
              }
              <option value="">None</option>
            </select>
            <input id="insert-changelog-button" type="button" value="Insert ChangeLog"/>
          </div>
          @gitbucket.core.helper.html.preview(
            repository         = repository,
            content            = content,
            enableWikiLink     = false,
            enableRefsLink     = true,
            enableLineBreaks   = true,
            enableTaskList     = true,
            hasWritePermission = true,
            completionContext  = "releases",
            style              = "height: 200px; max-height: 500px;",
            elastic            = true,
            placeholder        = "Describe this release",
            ariaLabel          = "Release content"
          )
          <ul id="assets-list" class="collaborator">
            @release.map { case (release, assets) =>
              @assets.map { asset =>
                <li>
                  <a href="@context.baseUrl/@repository.owner/@repository.name/_release/@helpers.urlEncode(tag.name)/@asset.fileName"><i class="octicon octicon-file"></i>@asset.label</a>
                  <a href="#" class="remove pull-right" style="padding-top: 0px;">(remove)</a>
                  <input type="hidden" name="file:@asset.fileName" value="@asset.label"/>
                </li>
              }
            }
          </ul>
          <div style="border: 1px dashed #ccc; color: gray; background-color: #eee; padding: 4px;">
            <div id="drop" class="clickable">Attach release files by dragging &amp; dropping, or selecting them.</div>
          </div>
          <div class="align-right" style="margin-top: 12px;">
            @if(release.isEmpty){
              <input type="submit" class="btn btn-success" value="Submit new release" formaction="@helpers.url(repository)/releases/@helpers.urlEncode(tag.name)/create"/>
            } else {
              <input type="submit" class="btn btn-success" value="Update release" formaction="@helpers.url(repository)/releases/@helpers.urlEncode(tag.name)/edit"/>
            }
          </div>
        </div>
      </div>
    </form>
  }
}
<script>
$(function(){
  $(document).on('click', '.remove', function(){
    $(this).parent().remove();
  });

  $("#drop").dropzone({
    url: '@context.path/upload/release/@repository.owner/@repository.name/@helpers.encodeRefName(tag.name)',
    maxFilesize: @{context.settings.upload.largeMaxFileSize / 1024 / 1024},
    timeout: @{context.settings.upload.largeTimeout},
    previewTemplate: "<div class=\"dz-preview\">\n  <div class=\"dz-progress\"><span class=\"dz-upload\" data-dz-uploadprogress>Uploading your files...</span></div>\n  <div class=\"dz-error-message\"><span data-dz-errormessage></span></div>\n</div>",
    success: function(file, id) {
      var attach =
        '<li><a href="@context.baseUrl/@repository.owner/@repository.name/_release/@helpers.encodeRefName(tag.name)/' + id + '">' +
        '<i class="octicon octicon-file"></i>' + escapeHtml(file.name) + '</a>' +
        '<a href="#" class="remove pull-right" style="padding-top: 0px;">(remove)</a>' +
        '<input type="hidden" name="file:' + id + '" value="' + escapeHtml(file.name) + '"/>'
        '</li>';
      $('#assets-list').append(attach);
      $(file.previewElement).prevAll('div.dz-preview').addBack().remove();
    }
  });

  $('#insert-changelog-button').click(function(){
    var previousTag = $('#insert-changelog-tag option:selected').val();
    $.get('@context.path/@repository.owner/@repository.name/changelog/' + encodeURIComponent(previousTag) + '...@helpers.urlEncode(tag.name)', function(data){
      console.log(data);
      var content = $('textarea[name=content]').val();
      if(content == ''){
        content = data;
      } else {
        content = content.trimRight() + '\n\n' + data;
      }
      $('textarea[name=content]').val(content);
    });
  });
});
</script>
